package com.wsy.controller;

import com.wsy.common.Res;
import com.wsy.entity.Line;
import com.wsy.service.AdminService;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@RestController
@RequestMapping("/api/admin")
public class AdminController {

    @Autowired
    AdminService adminService;


//    @PostMapping("/upload")
//    public void upload(MultipartHttpServletRequest request) {
//        Iterator<String> fileNames = request.getFileNames();
//        System.out.println(fileNames);
//        List<FileInfo> fileList = new ArrayList<>();
//        while (fileNames.hasNext()) {
//            MultipartFile multipartFile = request.getFile(fileNames.next());
//            FileInfo fileInfo = fileService.upload(multipartFile);
//            fileList.add(fileInfo);
//        }
//        if (fileList.size() == 1) {
//            return new Res(1, "上传成功", fileList.get(0));
//        } else {
//            return new Res(1, "上传成功", fileList);
//        }
//    }



    @GetMapping("/line")
    public List getLine(){
        List getLine = adminService.getLine();
        System.out.println(getLine);
        return getLine;
    }
    @GetMapping("/groupbuy")
    public List getGroupbuy(){
        List getLine = adminService.getGroupbuy();
        return getLine;
    }
    @GetMapping("/teamBuy")
    public List GetTeamBuy(){
        List getLine = adminService.GetTeamBuy();
        return getLine;
    }
    @GetMapping("/lineType")
    public List getLineType(){
        List getLine = adminService.getLineType();
        return getLine;
    }
    @PostMapping("/line")
    public Res PostLine(@RequestBody Line line){
        if (adminService.PostLine(line) != 0){
            return new Res(1,"添加成功");
        }else{
            return new Res(0,"添加失败");
        }
    }
//    修改线路信息
    @PutMapping("/line")
    public Res PutLine(@RequestBody Line line){
        if (adminService.PutLine(line) != 0){
            return new Res(1,"修改成功");
        }else{
            return new Res(0,"修改失败");
        }
    }

//    设置团购
    @PutMapping("/Groupbuy")
    public Res PutBuy(@RequestBody Line line){
        System.out.println(line);
        if (adminService.PutBuy(line) != 0){
            return new Res(1,"修改成功");
        }else{
            return new Res(0,"修改失败");
        }
    }

    @GetMapping("/order")
    public List<Line> getOrder(){
        return adminService.getOrder();
    }


    @DeleteMapping("/order/{id}")
    public Res delOrder(@PathVariable("id") int id){
        if (adminService.delOrder(id) != 0){
            return new Res(1,"删除成功");
        }else {
            return new Res(0,"删除失败");
        }
    }

    @DeleteMapping("/customer/{id}")
    public Res delCustomer(@PathVariable("id") int id){
        if (adminService.delCustomer(id) != 0){
            return new Res(1,"删除成功");
        }else {
            return new Res(0,"删除失败");
        }
    }

    @PostMapping("/customer")
    public List<Line> getCustomer(@RequestBody Line line){
        return adminService.getCustomer(line);
    }
}
